<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>系统用户管理</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0"> 
	<meta http-equiv="X-UA-Compatible" content="IE=edge" />
	<jsp:include page="../resource.jsp"></jsp:include>
	<style type="text/css">
	    #authority table, caption, tbody, tfoot, thead, tr, th, td{
	        vertical-align: middle;
	    }
	</style>
	<script type="text/javascript">
	$(document).ready(function() {
		var h = '85%';
 		if($.browser.msie && ($.browser.version == 8.0 || $.browser.version == 9.0)){
 	    	 h = $(self.parent.parent.frames['mainFrame']).height()-87; 
 	    }
        $('#users').omGrid({
        	dataSource : 'users!findAllUser.do',
            height : h,
            limit : 20,
            width : '99.8%',
            showIndex : true,
            colModel : [ 
                         {header : '用户名称', name : 'name', width : 60, align : 'left'},
                         {header : '系统登录名称', name : 'loginid', width : 90, align : 'left'},
                         {header : '部门', name : 'organizationName', width : 120, align : 'left'},
                         {header : 'priority', name : 'priority', width : 60, align : 'left'}, 
                         {header : 'guid', name : 'guid', width : 60, align : 'left'}, 
                         {header : '邮箱', name : 'mail', width : 120, align : 'left'}, 
                         {header : '移动电话', name : 'mobile', width : 120, align : 'left'}, 
                         {header : '用户状态', name : 'status', align : 'left', width : 'autoExpand'}]
        });
        
        $('#userEditor').omDialog({
        	autoOpen: false,
  		    height: 300,
  		    width : 480,
  		    buttons: [{
                text : "确定", 
                click : function () {
                  $('#userForm').submit();
                }
             }, {
                text : "取消", 
                click : function () {
                  $("#userEditor" ).omDialog("close");
              }
          }]
        });
        
        $('#orgId').bind('focus',function(){
        	$('#orgTreeDialog').omDialog("open");
        });
        //只绑定一次
        $('#orgId').one('focus',function(){
        	if($('#orgTree').html() == ''){
	        	$('#orgTree').omTree({
	                dataSource : 'users!queryOrgList.do'
	            });
        	}
        });
        
        
        $('#orgTreeDialog').omDialog({
        	autoOpen: false,
  		    height: 250,
  		    width : 280,
            modal : true,
  		    buttons: [{
	              text : "确定", 
	              click : function () {
	            	  var treeNode = $("#orgTree").omTree("getSelected");
	            	  $('#organizationId').val(treeNode.id);
	            	  $('#orgId').val(treeNode.text);
	                  $("#orgTreeDialog" ).omDialog("close");
	              }
	          }, {
	              text : "取消", 
	              click : function () {
	                  $("#orgTreeDialog" ).omDialog("close");
	              }
	          }]

        });
        
        function parse(v){
        	return (v == 'true')?'是':'否';
        }
        
        $('#userAdd').click(function(){
        	$('#userForm').resetForm();
        	$('#userId').val('');
        	$('#userEditor').omDialog("open");
        });
        
        $("#userDelete").click(function(){
        	var selects = $("#users").omGrid("getSelections",true);
        	if(selects.length <= 0){
        		$.omMessageBox.alert({content:'请您选择修改记录'});
        		return;
        	}
        	 $.omMessageBox.confirm({
                 title:'确认删除',
                 content:'您确定删除该用户？',
                 onClose:function(result){
                	 if(result){
                		 $.ajax({
                			 url : 'users!deleteUser.do',
                			 data : {userId:selects[0].id},
                			 success : function(data){
                				 if(data == '1'){
                					 $.omMessageTip.show({timeout : 2000,content : '删除成功！'});
                					 $('#users').omGrid("reload");
                				 }else{
                					 $.omMessageTip.show({timeout : 2000,content : '删除失败！'});
                				 }
                			 }
                		 });
                	 }
                 }
             });

        	
        });
        
        $('#userModify').click(function(){
        	var selects = $("#users").omGrid("getSelections",true);
        	if(selects.length <= 0){
        		$.omMessageBox.alert({content:'请您选择修改记录'});
        		return;
        	}
        	$('#userEditor').omDialog("open");
        	$('#userForm').resetForm();
        	$('#userPassword').val(selects[0].password);
        	$("#password").parent().parent().hide();
        	$('#name').val(selects[0].name);
        	$('#userId').val(selects[0].id);
        	$('#orgId').val(selects[0].organizationName);
        	$('#organizationId').val(selects[0].organizationId);
        	$('#loginid').val(selects[0].loginid);
        	$('#mobile').val(selects[0].mobile);
        	$('#mail').val(selects[0].mail);
        	$('#uid').val(selects[0].uid);
        	$('#contact').val(selects[0].contact);
        	$('#effect').val(selects[0].effect);
        });
        
        
        $('#userPasswordEditor').omDialog({
        	autoOpen: false,
  		    height: 150,
  		    width : 380,
  		  buttons: [{
              text : "确定", 
              click : function () {
                  $("#userPasswordForm" ).submit();
              }
          }, {
              text : "取消", 
              click : function () {
                  $("#userPasswordEditor" ).omDialog("close");
              }
          }]
        });
        
        $("#userPasswordModify").click(function(){
        	var selects = $("#users").omGrid("getSelections",true);
        	if(selects.length <= 0){
        		$.omMessageBox.alert({content:'请您选择修改记录'});
        		return;
        	}
        	$('#userPasswordForm').resetForm();
        	$('#cp_user_id').val(selects[0].id);
        	$('#userPasswordEditor').omDialog("open");
        });
        
        var userOptions  = {
      		  url : 'users!saveUser.do',
                success : function(data){
                   	if(data != '0'){  
                       	$.omMessageTip.show({timeout : 2000,content : '保存成功！'});
                       	$('#userEditor').omDialog('close');
                       	$('#users').omGrid("reload");
                   	}else{
                   		$.omMessageTip.show({timeout : 2000,content : '保存失败！',type : 'error'});
                   	}
                }
      	  };
      	  $('#userForm').submit(function() {
                $(this).omAjaxSubmit(userOptions);
                return false;//返回false,阻止浏览器默认行为
          });
      	  
      	var userPasswordOptions  = {
        		  url : 'users!savePassword.do',
                  success : function(data){
                     	if(data == '1'){  
                         	$.omMessageTip.show({timeout : 2000,content : '修改密码成功！'});
                         	$('#authorityDialog').omDialog('close');
                     	}else{
                     		$.omMessageTip.show({timeout : 2000,content : '修改失败！',type : 'error'});
                     	}
                  }
        	  };
      	  $('#userPasswordForm').submit(function() {
      		  if($("#passwordConfirm").val() != $("#password_1").val()){
      			$.omMessageTip.show({timeout : 2000,content : '两次密码不一致！',type : 'error'});
      			return false;
      		  }
                $(this).omAjaxSubmit(userPasswordOptions);
                return false;//返回false,阻止浏览器默认行为
          });
      	  
      	  $('#authorityDialog').omDialog({
      		  autoOpen: false,
  		      width : 580,
	  		  buttons: [{
	              text : "确定", 
	              click : function () {
	                  $('#authorityValue').val($('#authority').omItemSelector('value'));
	            	  $('#authorityForm').submit();
	            	  $("#authorityDialog" ).omDialog("close");
	              }
	          }, {
	              text : "取消", 
	              click : function () {
	                  $("#authorityDialog" ).omDialog("close");
	              }
	          }]
      	  });
      	  
      	$('#authorityForm').submit(function() {
              $(this).omAjaxSubmit({
            	  url : 'users!saveAuthority.do',
                  success : function(data){
                     	if(data == '1'){  
                         	$.omMessageTip.show({timeout : 2000,content : '设置成功！'});
                         	$('#userPasswordEditor').omDialog('close');
                     	}else{
                     		$.omMessageTip.show({timeout : 2000,content : '设置失败！',type : 'error'});
                     	}
                  }
              });
              return false;//返回false,阻止浏览器默认行为
        });
      	  
      	  $('#userAuthority').click(function(){
      		var selects = $("#users").omGrid("getSelections",true);
        	if(selects.length <= 0){
        		$.omMessageBox.alert({content:'请您选择授权的记录'});
        		return;
        	}
        	$('#userIdAuthority').val(selects[0].id);
      		$('#authorityDialog').omDialog('open');
      		$('#authority').omItemSelector({
                availableTitle : '未授权权限',
                selectedTitle : '已有权限',
                dataSource : 'users!findAuthority.do',
                toolbarIcons:[2,3,0,1,4],
                width:520
            });
      		 $.ajax({
                 url: 'users!findAuthoryByUserId.do',
                 method: 'POST',
                 data : {userId : selects[0].id},
                 dataType: 'text',
                 success: function(data, textStatus){
                	 if(data == "0"){
                		 $.omMessageBox.alert({content:'获取角色信息失败，请刷新页面重试！'});
               		     return;
                	 }
                	 var dataOjb = data.split(',');
                	 var a = [];
                	 for(var i=0 ;i<dataOjb.length ; i++){
                		 if(dataOjb[i] != null && dataOjb[i] != '')
                		 a.push(parseInt(dataOjb[i]));
                	 }
                	 $('#authority').omItemSelector('value',a);
                 },
                 error: function(XMLHttpRequest, textStatus, errorThrown){
                	 $.omMessageBox.alert({content:'获取角色信息失败，请刷新页面重试！'});
           		     return;
                 }
             });
      	  });
    });
	function checkData(){
		 var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
		 if(!myreg.test($('#mail').val())) {
			 alert("请输入正确的邮箱地址");
		 }
	}
	</script>  
  </head>
  
  <body>
    <div class="toolbar mar">
		         <a href="#" class="rbutton" id="userAdd"><span><img src="../images/add.gif" width="14" height="14" /><ins>添加</ins></span></a>
		         <a  class="line"></A>
		         <a href="#" class="rbutton" id="userModify"><span><img src="../images/add.gif" width="14" height="14" /><ins>修改</ins></span></a>
		         <a  class="line"></A>
		         <a href="#" class="rbutton" id="userPasswordModify"><span><img src="../images/add.gif" width="14" height="14" /><ins>修改密码</ins></span></a>
		         <a  class="line"></A>
		         <a href="#" class="rbutton" id="userDelete"><span><img src="../images/del.gif" width="14" height="14" /><ins>删除</ins></span></a>
		         <a  class="line"></A>
		         <a href="#" class="rbutton" id="userAuthority"><span><img src="../images/add.gif" width="14" height="14" /><ins>授权</ins></span></a>
	</div>
    <table id="users"></table>
    
    <div id="userEditor" title="编辑用户" style="display: none;">
     <form id="userForm" method="post">
          <table width="100%" border="0" class="grid_layout" cellspacing="0">
             <tr>
               <td width="10%" align="right">用户名：</td>
               <td width="28%" colspan="3">
                 <input type="hidden" id="userId" name="user.id">
                 <input type="text" name="user.name" id="name" class="input_text"/>
               </td>
             </tr>
             <tr>
               <td width="10%" align="right">系统登录名：</td>
               <td width="28%" colspan="3">
                 <input type="text" name="user.loginid" id="loginid" class="input_text"/>
               </td>
             </tr>
             <tr>
               <td width="10%" align="right">密码：</td>
               <td width="28%" colspan="3">
                 <input type="password"" name="user.password" id="password" class="input_text"/>
               </td>
             </tr>
             <tr>
               <td width="10%" align="right">部门：</td>
               <td width="28%" colspan="3">
                  <input type="text" id="organizationId" name="user.organizationId" style="display: none;">
                  <input type="text" id="orgId" class="input_text" readonly="readonly">
               </td>
             </tr>
             <tr>
               <td width="10%" align="right">移动电话：</td>
               <td width="28%" colspan="3">
                  <input type="text" name="user.mobile" id="mobile" class="input_text"/>
               </td>
             </tr>
             <tr>
               <td width="10%" align="right">E-MAIL：</td>
               <td width="28%" colspan="3">
                  <input type="text" name="user.mail" id="mail" class="input_text" onblur="checkData()"/>
               </td>
             </tr>
             <tr>
               <td align="right">是否有效：</td>
               <td colspan="3">
                  <select name="user.status" id="status" class="input_slelct">
	                  <option value="true">有效</option>
	                  <option value="false">无效</option>
                  </select>
               </td>
             </tr>
           </table>
         </form>
   </div>
   <div id="userPasswordEditor" title="修改密码" style="display: none;">
     <form id="userPasswordForm" method="post">
          <table width="100%" border="0" class="grid_layout" cellspacing="0">
             <tr>
               <td width="10%" align="right">请输入新密码：</td>
               <td width="28%" colspan="3">
                 <input type="hidden" name="cp_user_id" id="cp_user_id" class="input_text"/>
                 <input type="password" name="password_1" id="password_1" class="input_text"/>
               </td>
             </tr>
             <tr>
               <td width="10%" align="right">请再次输入：</td>
               <td width="28%" colspan="3">
                 <input type="password"" name="passwordConfirm" id="passwordConfirm" class="input_text"/>
               </td>
             </tr>
           </table>
         </form>
   </div>
   
   <div id="orgTreeDialog" title="部门选择" style="display: none;">
      <ul id="orgTree"></ul>
   </div>
   
   <div id="authorityDialog" title="授权" style="display: none;">
     <form id="authorityForm">
      <input type="hidden" name="userIdAuthority" id="userIdAuthority"  />
      <input type="hidden" name="authorityValue" id="authorityValue" />
      <div id="authority"></div>
     </form>
   </div>
  </body>
</html>
